package com.example.base.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.base.excetion.GlobalExcetion;
import com.example.base.system.domain.SysTableColumn;
import com.example.base.system.domain.vo.SysTableColumnVo;
import com.example.base.system.mapper.SysTableColumnMapper;
import com.example.base.system.service.SysTableColumnService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author X.I.O
 * @since 2024-02-21
 */
@Service
@Transactional
public class SysTableColumnServiceImpl extends ServiceImpl<SysTableColumnMapper, SysTableColumn> implements SysTableColumnService {


    @Override
    public SysTableColumn getById(Serializable id){
        return super.getById(id);
    }


    @Override
    public boolean removeById(Serializable id) {
        return super.removeById(id);
    }


    @Override
    public boolean updateById(SysTableColumn systablecolumn) {
        return super.updateById(systablecolumn);
    }


    public SysTableColumn saveSysTableColumn(SysTableColumn systablecolumn) throws GlobalExcetion {
        super.save(systablecolumn);
        return systablecolumn;
    }


    public SysTableColumn updateSysTableColumnById(SysTableColumn systablecolumn) throws GlobalExcetion{
        super.updateById(systablecolumn);
        return systablecolumn;
    }


    public SysTableColumn saveOrUpdateSysTableColumn(SysTableColumnVo systablecolumnVo) throws GlobalExcetion{
        SysTableColumn systablecolumn=new SysTableColumn();
        BeanUtils.copyProperties(systablecolumnVo,systablecolumn);
        if(systablecolumnVo.getId()!=null){
            SysTableColumn systablecolumnEntity=super.baseMapper.selectById(systablecolumnVo.getId());
            if(systablecolumnEntity==null){
                throw new GlobalExcetion(1,"数据不存在");
            }
            systablecolumn.setUpdatetime(new Date());
            super.baseMapper.updateById(systablecolumn);
        }else{
            systablecolumn.setDeleted(0);
            systablecolumn.setCreatetime(new Date());
            systablecolumn.setUpdatetime(new Date());
            super.baseMapper.insert(systablecolumn);
        }
        return systablecolumn;
    }

    @Override
    public List<SysTableColumn> getSysTableColumnByTableId(Serializable tableId) {
        LambdaQueryWrapper<SysTableColumn> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SysTableColumn::getTableId,tableId);
        return super.baseMapper.selectList(lambdaQueryWrapper);
    }
}
